package com.ricchieri.manage.bean;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.TripFilter;
import com.ricchieri.lazymodel.TripLazyDataModel;
import com.ricchieri.model.Trip;
import com.ricchieri.service.interf.IPlaceService;
import com.ricchieri.service.interf.ITripService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "searchTripMB")
@ViewScoped
public class SearchTripManageBean extends GenericManageBean {

	private static final long serialVersionUID = -275710803306101330L;
	
	@ManagedProperty(value = "#{PlaceService}")
	IPlaceService placeService;
	
	@ManagedProperty(value = "#{TripService}")
	ITripService tripService;
	
	private TripFilter tripFilter;
	private Map<String,String> places = null;
	private TripLazyDataModel tripLazyDataModel;
	
	@PostConstruct
	public void init() {
		tripFilter = (TripFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(tripFilter == null) {
			tripFilter = new TripFilter();
		}
	}
	
	public IPlaceService getPlaceService() {
		return placeService;
	}
	
	public void setPlaceService(IPlaceService placeService) {
		this.placeService = placeService;
	}
	
	public TripFilter getTripFilter() {
		return tripFilter;
	}
	
	public void setTripFilter(TripFilter tripFilter) {
		this.tripFilter = tripFilter;
	}
	
	public Map<String, String> getPlaces() {
		if(places == null) {
			places = getPlaceService().getComboPlaces();
		}
		return places;
	}
	
	public void setPlaces(Map<String, String> places) {
		this.places = places;
	}
	
	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}

	public void search() {
		tripLazyDataModel = null;
	}
	
	public void reset() {
		tripFilter = new TripFilter();
		tripLazyDataModel = null;
	}

	public TripLazyDataModel getTripLazyDataModel() {
		
		if(tripLazyDataModel == null) {
			tripLazyDataModel = new TripLazyDataModel(tripService, tripFilter);
		}
		return tripLazyDataModel;
	}

	public void setTripLazyDataModel(TripLazyDataModel tripLazyDataModel) {
		this.tripLazyDataModel = tripLazyDataModel;
	}
	
	public String viewTrip(Trip trip) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_TRIP, trip);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, tripFilter);
		return BusinessSettings.VIEW_DETAILS;
	}
}

